package com.mes.cloud.service;

import com.mes.cloud.api.dto.request.position.SysPositionChannelTypeRequest;
import com.mes.cloud.api.dto.request.position.SysPositionOrgAuthRequest;
import com.mes.cloud.api.dto.response.position.SysPositionChannelTypeDto;
import com.mes.cloud.common.JsonResult;
import com.mes.cloud.constant.Constants;
import com.mes.cloud.dao.SysPositionChannelTypeDao;
import com.mes.cloud.entity.SysPositionChannelType;
import com.mes.cloud.enums.HttpStatusEnum;
import com.mes.cloud.util.DateUtil;
import com.mes.cloud.util.UserUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 岗位客户类型中间表
 *
 * @author xy
 * @version 2019-08-06 16:39:01
 * @email yuan.xue3@pactera.com
 */
@Service
public class SysPositionChannelTypeService {

    private SysPositionChannelTypeDao sysPositionChannelTypeDao;

    private IPKeyGenerator ipKeyGenerator;

    @SuppressWarnings("SpringJavaAutowiringInspection")
    @Autowired
    public SysPositionChannelTypeService(SysPositionChannelTypeDao sysPositionChannelTypeDao, IPKeyGenerator ipKeyGenerator) {
        this.sysPositionChannelTypeDao = sysPositionChannelTypeDao;
        this.ipKeyGenerator = ipKeyGenerator;
    }

    /**
     * <查询列表>
     *
     * @param sysPositionChannelTypeRequest
     * @return com.shinho.tntframe.core.common.model.JsonResult<java.util.List<com.shinho.crm.base.api.dto.response.position.SysPositionCustomerTypeDto>>
     * @author xy
     * @date 2019/8/9 10:21
     */
    public JsonResult<List<SysPositionChannelTypeDto>> list(SysPositionChannelTypeRequest sysPositionChannelTypeRequest) {

        //查询条件
        SysPositionChannelType selectParam = new SysPositionChannelType();
        BeanUtils.copyProperties(sysPositionChannelTypeRequest, selectParam);
        selectParam.setDelFlag(Constants.DEL_FLAG_NO);
        List<SysPositionChannelType> sysPositionChannelTypeVoList = sysPositionChannelTypeDao.findAllChannelType(selectParam);
        List<SysPositionChannelTypeDto> sysPositionChannelTypeDtoList = new ArrayList<>();
        for (SysPositionChannelType sysPositionChannelType : sysPositionChannelTypeVoList) {
            SysPositionChannelTypeDto sysPositionChannelTypeDto = new SysPositionChannelTypeDto();
            BeanUtils.copyProperties(sysPositionChannelType, sysPositionChannelTypeDto);
            sysPositionChannelTypeDtoList.add(sysPositionChannelTypeDto);
        }
        JsonResult<List<SysPositionChannelTypeDto>> jsonResult = new JsonResult<>();
        jsonResult.setMsg(HttpStatusEnum.OK.getMessage()).setCode(HttpStatusEnum.OK.getCode()).setData(sysPositionChannelTypeDtoList);
        return jsonResult;
    }


    /**
     * <批量新增>
     *
     * @param sysPositionOrgAuthRequest
     * @return com.shinho.tntframe.core.common.model.JsonResult
     * @author xy
     * @date 2019/8/6 17:18
     */
    @Transactional(rollbackFor = Exception.class)
    public JsonResult batchInsert(SysPositionOrgAuthRequest sysPositionOrgAuthRequest) {
        String positionId = sysPositionOrgAuthRequest.getPositionId();
        //查询岗位是否已经有数据
        SysPositionChannelType queryParams = new SysPositionChannelType();
        queryParams.setPositionId(positionId);
        queryParams.setDelFlag(Constants.DEL_FLAG_NO);
        List<SysPositionChannelType> list = sysPositionChannelTypeDao.select(queryParams);
        List<String> customerTypeIds = sysPositionOrgAuthRequest.getCustomerTypeIds();
        //如果已有数据更新数据状态
        if (!CollectionUtils.isEmpty(customerTypeIds) || !CollectionUtils.isEmpty(list)) {
            this.updateByPositionId(sysPositionOrgAuthRequest);
        }
        JsonResult jsonResult = new JsonResult();
        //如果提交数据不为空
        if (!CollectionUtils.isEmpty(customerTypeIds) && customerTypeIds.size() > 0) {
            List<SysPositionChannelType> spct = new ArrayList<>();
            for (String channelId : customerTypeIds) {
                SysPositionChannelType sysPositionChannelType = new SysPositionChannelType();
                sysPositionChannelType.setId(ipKeyGenerator.getGenerateKey());
                sysPositionChannelType.setPositionId(positionId);
                sysPositionChannelType.setChannelId(channelId);
                sysPositionChannelType.setUpdateDateStr(DateUtil.updateDateStr());
                sysPositionChannelType.setCreateBy(UserUtil.getCreateByName());
                sysPositionChannelType.setUpdateBy(UserUtil.getUpdateByName());
                sysPositionChannelType.setCreateByNo(UserUtil.getCreateByNo());
                sysPositionChannelType.setUpdateByNo(UserUtil.getUpdateByNo());
                spct.add(sysPositionChannelType);
            }
            int count = sysPositionChannelTypeDao.batchInsert(spct);
            if (count > 0) {
                jsonResult.setMsg(HttpStatusEnum.OK.getMessage()).setCode(HttpStatusEnum.OK.getCode());
            }
        }
        return jsonResult;
    }

    private void updateByPositionId(SysPositionOrgAuthRequest sysPositionOrgAuthRequest) {
        sysPositionOrgAuthRequest.setPositionId(sysPositionOrgAuthRequest.getPositionId());
        sysPositionOrgAuthRequest.setUpdateBy(UserUtil.getUpdateByName());
        sysPositionOrgAuthRequest.setUpdateByNo(UserUtil.getUpdateByNo());
        sysPositionOrgAuthRequest.setUpdateDateStr(DateUtil.updateDateStr());
        sysPositionOrgAuthRequest.setDelFlag(Constants.DEL_FLAG_YES);
        sysPositionChannelTypeDao.updateByPositionId(sysPositionOrgAuthRequest);
    }
}
